<?php

class ShoppingCartController extends GxController {

    public function actionView() {
        $this->layout = 'shop_cart';
        $AllDataCart = Yii::app()->shoppingCart->getPositions();
        $this->render('_view', array(
            'AllDataCart' => $AllDataCart,
        ));
    }

    /* Add to cart */

    public function actionCreate() {
        if ($_POST['class_course_type'] == "course") {
            $ClassBatch = CourseBatche::model()->findByPk($_POST['ShoppingCart']['batch_id']);
        } else {
            $ClassBatch = ClassBatch::model()->findByPk($_POST['ShoppingCart']['batch_id']);
        }
        Yii::app()->shoppingCart->put($ClassBatch, $_POST['ShoppingCart']['booking_seats'], $_POST['ShoppingCart']['booking_date'], $_POST['ShoppingCart']['batchTime'], $_POST['ShoppingCart']['booking_price']);
        echo "success";
    }

    /* Add to cart update */

    public function actionUpdate() {
        $booking_price = '';
        if ($_POST['classcoursetype'] == "classes") {
            $ClassBatch = ClassBatch::model()->findByPk($_POST['batchId']);
        } else {
            $ClassBatch = CourseBatche::model()->findByPk($_POST['batchId']);
        }
        if ($_POST['qty'] <= $ClassBatch->seats) {
            $booking_price = ClassBatch::getBatchPriceBySeatsInt($_POST['batchId'], $_POST['qty'], 'int');
            Yii::app()->shoppingCart->put($ClassBatch, $_POST['qty'], $_POST['batchtiming'], $_POST['startdate'], $booking_price);
            echo "success";
        } else {
            echo $ClassBatch->seats . "#@#" . $_POST['qty'];
        }
    }

    /* Add to cart remove */

    public function actionDelete() {
        Yii::app()->shoppingCart->remove($_POST['batchId']);
        if (count(Yii::app()->shoppingCart->getPositions()) == 0) {
            Yii::app()->session['totalamtsession'] = '';
            Yii::app()->session['appliedcoupon'] = '';
        }
        echo "success";
    }

    /* collect info from response EBS */

    public function actionResponseEBS() {
         $this->layout = 'detailspage';
        $secret_key = Message::SECRETKEY;
        $couponid = '';
        $vanueAddress = '';
        $earlyDscount = '';
        $groupDscount = '';
        $cost = '';
        $couponcode = '';
        if (isset($_GET['DR'])) {
            $DR = preg_replace("/\s/", "+", $_GET['DR']);
            $rc4 = new Crypt_RC4($secret_key);
            $QueryString = base64_decode($DR);
            $rc4->decrypt($QueryString);
            $QueryString = explode('&', $QueryString);
            $response = array();
            foreach ($QueryString as $param) {
                $param = explode('=', $param);
                $response[$param[0]] = urldecode($param[1]);
            }
            if ($response['ResponseCode'] == '0') {
                if (Yii::app()->user->hasState('appliedcoupon') <> "") {
                    $couponcode = Yii::app()->user->hasState('appliedcoupon');
                     $coupon = Coupons::getCouponType($couponcode);
                    $couponid = !empty($coupon)?$coupon->id:'';
                }
                $purchaseOrderModel = new PurchaseOrder;
                $purchaseOrderModel->setAttribute('transaction_id', $response['TransactionID']);
                $purchaseOrderModel->setAttribute('total_price', $response['Amount']);
                $purchaseOrderModel->setAttribute('order_date_time', $response['DateCreated']);
                $purchaseOrderModel->setAttribute('users_id', getLoginUserId());
                $purchaseOrderModel->setAttribute('order_confirmed', SkillConfig::STATUS_ACTIVE);
                $purchaseOrderModel->setAttribute('coupons_code', $couponid);
                if ($purchaseOrderModel->save()) {
                    $AllDataCart = Yii::app()->shoppingCart->getPositions();
                    foreach ($AllDataCart as $AllDataCart) {
                        $PurchaseClassCourseModel = new PurchaseClassCourse;
                        $PurchaseClassCourseModel->setAttribute('purchaseorder_id', $purchaseOrderModel->id);
                        $PurchaseClassCourseModel->setAttribute('batch_start_date', $AllDataCart->startdate);
                        $PurchaseClassCourseModel->setAttribute('batch_start_time', $AllDataCart->batchtiming);

                        $PurchaseClassCourseModel->setAttribute('batch_seats', $AllDataCart->quantity);
                        if ($AllDataCart->classes_id <> "") {
                            $batchPrice = ClassBatch::getBatchPricebyBatchId($AllDataCart->id);
                            $vanueAddress = ClassVenue::getVenueAddressVyClassCoursebatchId('class', $AllDataCart->id);
                            $PurchaseClassCourseModel->setAttribute('classes_id', $AllDataCart->classes_id);
                            $PurchaseClassCourseModel->setAttribute('class_batch_id', $AllDataCart->id);
                        } else {
                            $vanueAddress = ClassVenue::getVenueAddressVyClassCoursebatchId('course', $AllDataCart->id);
                            $PurchaseClassCourseModel->setAttribute('courses_id', $AllDataCart->classes_id);
                            $PurchaseClassCourseModel->setAttribute('course_batch_id', $AllDataCart->id);
                        }
                        $PurchaseClassCourseModel->setAttribute('batch_price', $batchPrice);
                        $earlyDscount = ClassBatch::getearlyDscountByBatch($AllDataCart->id, $AllDataCart->quantity);
                        $groupDscount = ClassBatch::getgroupDscountByBatch($AllDataCart->id, $AllDataCart->quantity);
                        $PurchaseClassCourseModel->setAttribute('batch_early_dicount', $earlyDscount);
                        $PurchaseClassCourseModel->setAttribute('batch_group_discount', $groupDscount);
                        $PurchaseClassCourseModel->setAttribute('venue_deatils', $vanueAddress);
                        $PurchaseClassCourseModel->save();
						
                    }
                }
			}
        }
        $this->render('_response', array('orderId' => $purchaseOrderModel->id, 'totalCost' => $purchaseOrderModel->total_price));
    }

    /* Add to cart remove */

    public function actionAddDiscount() {
        $date = date('Y-m-d');
        $shopinCartclassCourseId = '';
        $valid_err = '';
        $couponModel = Coupons::model()->findByAttributes(array('name' => trim($_POST['discount_code']), 'status' => SkillConfig::STATUS_ACTIVE));
        if (!empty($couponModel)) {
            $couponError = Coupons::getCouponRescrtion(Yii::app()->user->setState('appliedcoupon', trim($_POST['discount_code'])));
            if ($couponError != 'false') {
                Yii::app()->user->setState('appliedcoupon', trim($_POST['discount_code']));
                echo SkillConfig::STATUS_ACTIVE;
            } else {
                echo 'false';
            }
        } else {
            echo 'Invalid Coupon';
        }
    }

}